package net.neptech.controller;

import net.neptech.model.sys.Dept;
import net.neptech.page.PageQuery;
import net.neptech.page.TableDataInfo;
import net.neptech.service.DeptService;
import net.neptech.vm.ResponseResult;
import net.neptech.vo.TreeSelectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: liyuxiang
 * @Date: 2025/8/15 11:03
 * @Description: 部门信息
 */
@RestController
@RequestMapping("/system/dept")
public class DeptController {
    @Autowired
    DeptService deptService;

    /**
     * 获取部门列表
     */
    @GetMapping("/list")
    public ResponseResult<List<Dept>> list(Dept dept) {
        List<Dept> depts = deptService.selectDeptList(dept);
        return ResponseResult.success(depts);
    }

    /**
     * 获取部门下拉树结构
     */
    @GetMapping("/deptTree")
    public ResponseResult<List<TreeSelectVo>> deptTree(Dept dept) {
        List<TreeSelectVo> treeSelectVos = deptService.selectDeptTree(dept);
        return ResponseResult.success(treeSelectVos);

    }

    /**
     * 获取部门信息
     */
    @GetMapping("/{deptId}")
    public ResponseResult<Dept> selectDeptById(@PathVariable("deptId") Long deptId) {
        Dept dept = deptService.selectDeptById(deptId);
        return ResponseResult.success(dept);
    }

    @PutMapping("")
    public ResponseResult<Void> updateDept(@RequestBody Dept dept) {
        deptService.updateDept(dept);
        return ResponseResult.success();
    }

    @PostMapping("")
    public ResponseResult<Void> insertDept(@RequestBody Dept dept) {
        deptService.insertDept(dept);
        return ResponseResult.success();
    }

    @DeleteMapping("{deptId}")
    public ResponseResult<Void> deleteDeptById(@PathVariable("deptId") Long deptId) {
        deptService.deleteDeptById(deptId);
        return ResponseResult.success();
    }

    /**
     * 医生端获取部门列表，根据leader获取
     */
    @GetMapping("getDeptPage")
    public TableDataInfo<Dept> getDeptPage(Dept dept, PageQuery pageQuery) {
        TableDataInfo<Dept> deptList = deptService.getDeptPage(dept, pageQuery);
        return deptList;
    }

    /**
     * 查询1级科室
     */
    @GetMapping("/getOneDeptList")
    public ResponseResult<List<Dept>> getOneDeptList() {
        List<Dept> deptList = deptService.selectOneLevelDept();
        return ResponseResult.success(deptList);
    }

    /**
     * 查询旗下子科室
     */
    @GetMapping("/getChildDeptList/{parentId}")
    public ResponseResult<List<Dept>> getDeptByParentId(@PathVariable("parentId") Long parentId) {
        List<Dept> deptList = deptService.selectDeptByParentId(parentId);
        return ResponseResult.success(deptList);
    }

    /**
     * 根据科室名称获取科室详情
     */
    @GetMapping("getDeptDetailByDeptName")
    public ResponseResult<List<Dept>> getDeptDetailByDeptName(String deptName) {
        List<Dept> depts = deptService.getDeptDetailByDeptName(deptName);
        return ResponseResult.success(depts);
    }

    /**
     * 获取科室详情，ids
     */
    @GetMapping("getDeptDetailByDeptIds")
    public ResponseResult<List<Dept>> getDeptDetailByDeptIds(@RequestParam("deptIds") List<Long> deptIds) {
        List<Dept> deptList = deptService.getDeptDetailByDeptIds(deptIds);
        return ResponseResult.success(deptList);
    }

    @GetMapping("dept/getDepts")
    ResponseResult<List<Dept>> getDeptByDeptIds(@RequestParam("deptIds") List<String> deptIds){
        List<Dept> deptList = deptService.getDeptDetailByDeptIds(deptIds.stream().map(Long::getLong).toList());
        return ResponseResult.success(deptList);
    }

    @GetMapping("getAll")
    public ResponseResult<List<Dept>> getAllDept() {
        return ResponseResult.success(deptService.getAll());
    }
}
